#include <iostream>
#include <string>
#include <cmath>


// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp
#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
#include <boost/foreach.hpp>

// my functions
#define DEBUG_LEVEL 6
#include <debug_func.h>
#include <open_stream_name_with_subscript.h>
#include <print_error.h>
#include <program_options_helper.h>

#include "get_arg.h"

using std::string;
using boost::split;
using boost::is_any_of;
using std::cerr;
using std::vector;
using namespace program_options_helper;
namespace po = boost::program_options_lg_lg;


//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{

	//
	//	Positional arguments
	//
	po::positional_options_description positional_parser;
	positional_parser.add("input_files", -1);

	po::options_description positional_args("Positional Arguments");
	positional_args.add_options()
	("input_files,i",	po::value<vector<string> >(&args.input_files)
											->set_name("FILES"),
											"FILE NAMES.");


	std::vector<std::string> custom_flags;
	args.optional_args.add_options()
	("path,p", po::value<string>(&args.path)
											->default_value("[PATH][EXT]")
											->set_name("PATH"),
									"Output path.")
	("custom_flags,c", po::value<vector<string> >(&custom_flags)
											->set_name("flags"),
											"Each flag should be a pair of strings.\1"
											"All instances of the first will be be converted "
											"into the second.");

	args.add_std_options();

	//	Parse all arguments
	//
	po::options_description all_args("");
	all_args

				//	Mandatory arguments
				//.add(mandatory_args)

				//	Optional arguments
				.add(args.optional_args)

				//	Positional arguments
				.add(positional_args);

	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv)
								.options(all_args)

								//	Positional arguments
								.positional(positional_parser)

								.run(), vm);
	po::notify(vm);




	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.0");
		return false;
	}
	string exe_description = "Tests substitute stream name";
	if (vm.count("help"))
	{
		print_usage(cerr, string(*argv), "[OPTIONS]",
							all_args, exe_description, 80);
		return false;
	}

	if (vm.count("input_files") == 0)
	{
		std_print_error(std::cerr,  "Please specify some file names on the command line.");
		print_usage(cerr, string(*argv), "[OPTIONS]",
							all_args, exe_description, 80);
		return false;
	}
	if (vm.count("custom_flags"))
	{
		BOOST_FOREACH(const string& s, custom_flags) 
		{
			vector< string > results;
			split( results, s, is_any_of(",") );
			if (results.size() == 0)
				throw std::runtime_error ("Empty string passed as --custom_flags");
			if (results.size() == 1)
				throw std::runtime_error ("String \"" + s + "\" missing \",\" in  --custom_flags");
			if (results.size() > 2)
				throw std::runtime_error ("String \"" + s + "\" has too many commas in  --custom_flags");
			args.custom_flags.push_back(std::make_pair(results[0],  results[1]));
		}
	}

	args.open_err_log();
	return true;
}








